package com.shen.accountbook.bean.temp.biz;

import com.shen.accountbook.bean.temp.FirstSortBean;
import com.shen.accountbook.greendao.DaoSession;
import com.shen.accountbook.greendao.FirstSortBeanDao;
import com.shen.accountbook.greendao.manager.DaoManager;

import java.util.List;

/**
 * author:  shen
 * date:    2018/9/7
 */
public class FirstSortBeanBiz {

    DaoManager mDaoManager;
    DaoSession mDaoSession;
    FirstSortBeanDao mDao;

    public FirstSortBeanBiz(){
        mDaoManager = DaoManager.getInstance();
        mDaoSession = mDaoManager.getSession();
        mDao = mDaoSession.getFirstSortBeanDao();
    }

    /**
     * 插入分类
     * @param id
     * @param name
     */
    public long insert(long id, String name){

        long rowId = -1;
        List<FirstSortBean> list = mDao.queryBuilder().where(
                FirstSortBeanDao.Properties.First_sort_id.eq(id)).list();
        if (null == list || list.isEmpty()) {

            FirstSortBean bean = new FirstSortBean();
            bean.setId(null);
            bean.setFirst_sort_id(id);
            bean.setFirst_sort_name(name);
            rowId = mDao.insert(bean);

        } else {
            rowId = list.get(0).getId();
//            AcceptanceBean entity = list.get(0);
//
//            entity.setUserNumber( bean.getUserNumber());
//            entity.setAssetNumbers( bean.getAssetNumbers());
//            entity.setUserName( bean.getUserName());
//            entity.setMeterAddr( bean.getMeterAddr());
//            entity.setTerminalNo( bean.getTerminalNo());
//            entity.setMonthFreezingTimeIn( bean.getMonthFreezingTimeIn());
//            entity.setElectricityInByMonth( bean.getElectricityInByMonth());
//
//            mAcceptanceBeanDao.update(entity);
        }

        return rowId;
    }

    /**
     * 1
     *
     * 插入分类 -- 根据"first_sort_id"加1后 作为first_sort_id插入
     * @param name
     */
    public boolean insertAuto(String name){

        long rowId;
        List<FirstSortBean> list = mDao.queryBuilder()
                .orderDesc(FirstSortBeanDao.Properties.First_sort_id).list();
        if (null == list || list.isEmpty()) {

            FirstSortBean bean = new FirstSortBean();
            bean.setId(null);
            bean.setFirst_sort_id(3l);
            bean.setFirst_sort_name(name);
            rowId = mDao.insert(bean);

        } else {
            FirstSortBean beanTemp = list.get(0);

            FirstSortBean bean = new FirstSortBean();
            bean.setId(null);
            bean.setFirst_sort_id(beanTemp.getFirst_sort_id() + 1);
            bean.setFirst_sort_name(name);
            rowId = mDao.insert(bean);
        }

        if(rowId > 0){
            return true;
        }else {
            return false;
        }
    }

    /**
     * 获取全部的
     * @return
     */
    public List<FirstSortBean> getFirstSortBean(){
        List<FirstSortBean> beanList = mDao.queryBuilder().list();
        return beanList;
    }

    /**
     * 20个 为一页
     * @param offset
     * @return
     */
    public List<FirstSortBean> getFirstSortBean(int offset){
        List<FirstSortBean> beanList = mDao.queryBuilder().offset(offset * 20).limit(20).list();
        return beanList;
    }

}
